import base64
import requests
from fontTools.ttLib import TTFont


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
    'Cookie': 'sessionid=9x4ua98kwesnznrhkoy8wnoaal84hdw9;'
}
url = 'https://match.yuanrenxue.cn/api/match/7?page=%s'
r_map = {'unia813': 3, 'unia643': 2, 'unic195': 6, 'unib671': 5, 'unic268': 0, 'unib519': 4, 'unia594': 1, 'unib713': 9,
       'unia261': 8, 'unib741': 7}  # 数字映射字典
name = ['极镀ギ紬荕', '爷灬霸气傀儡', '梦战苍穹', '傲世哥', 'мaη肆風聲', '一刀メ隔世', '横刀メ绝杀', 'Q不死你R死你', '魔帝殤邪', '封刀不再战', '倾城孤狼', '戎马江湖',
            '狂得像风', '影之哀伤', '謸氕づ独尊', '傲视狂杀', '追风之梦', '枭雄在世', '傲视之巅', '黑夜刺客', '占你心为王', '爷来取你狗命', '御风踏血', '凫矢暮城', '孤影メ残刀',
            '野区霸王', '噬血啸月', '风逝无迹', '帅的睡不着', '血色杀戮者', '冷视天下', '帅出新高度', '風狆瑬蒗', '灵魂禁锢', 'ヤ地狱篮枫ゞ', '溅血メ破天', '剑尊メ杀戮',
            '塞外う飛龍', '哥‘K纯帅', '逆風祈雨', '恣意踏江山', '望断、天涯路', '地獄惡灵', '疯狂メ孽杀', '寂月灭影', '骚年霸称帝王', '狂杀メ无赦', '死灵的哀伤', '撩妹界扛把子',
            '霸刀☆藐视天下', '潇洒又能打', '狂卩龙灬巅丷峰', '羁旅天涯.', '南宫沐风', '风恋绝尘', '剑下孤魂', '一蓑烟雨', '领域★倾战', '威龙丶断魂神狙', '辉煌战绩', '屎来运赚',
            '伱、Bu够档次', '九音引魂箫', '骨子里的傲气', '霸海断长空', '没枪也很狂', '死魂★之灵']
result_num = []


def base_font_map():
    """构造基本on数组映射模板"""
    ttobj = TTFont('base.woff')
    tt_names = ttobj.getGlyphNames()[1:]    # 获取所有name值
    cmap = {}
    for i in tt_names:
        cmap[tuple(ttobj['glyf'][i].flags)] = i # 构造基本映射on数组模板
    return cmap


def aim_font(url):
    """目标字体文件映射处理"""
    res_num = {}    # 最新请求的明文映射
    cmap = base_font_map()
    response = requests.get(url, headers=headers)
    res_data = response.json()
    woff = res_data['woff']
    woff_file = base64.b64decode(woff.encode())
    with open('aim.woff', 'wb') as f:
        f.write(woff_file)
    ttobj_aim = TTFont('aim.woff')
    tt_names_aim = ttobj_aim.getGlyphNames()[1:]
    for i in tt_names_aim:
        # print(tuple(ttobj_aim['glyf'][i].flags))
        # print('基本模板的name', cmap[tuple(ttobj_aim['glyf'][i].flags)])
        # print('当前请求的name', i)
        res_num[i] = r_map[cmap[tuple(ttobj_aim['glyf'][i].flags)]]     # 将当前请求的name对应的每一个on数组进行明文映射
    # print(res_num)
    return res_num, res_data


def data_deal(url):
    """响应数据处理"""
    res_num, res_data = aim_font(url)
    data = res_data.get('data')
    for i in data:
        # print(i)
        i = i['value'].replace(' ', '')
        value_lst = i.split('&#x')[1:]
        nums = []
        for j in value_lst:
            nums.append(str(res_num[str('uni'+j)]))
        result_num.append(int(''.join(nums)))


if __name__ == '__main__':
    for i in range(1, 6):
        data_deal(url%i)
    result = tuple(zip(name[1:], result_num))
    result = sorted(result, key=lambda i:i[1], reverse=True)
    print(result[0])

